package features.advance.quartz.demo;

import features.advance.quartz.job.DumbJob;
import features.advance.quartz.job.HelloJob;
import features.advance.quartz.listeners.MyJobListener;
import features.advance.quartz.listeners.MySchedListener;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.calendar.HolidayCalendar;
import org.quartz.impl.matchers.KeyMatcher;

import java.util.Calendar;
import java.util.Date;

import static org.quartz.CronScheduleBuilder.dailyAtHourAndMinute;
import static org.quartz.DateBuilder.dateOf;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;
/**
 * @author LIN
 * @date 2022-05-12 16:24
 */
public class Demo {

    public static void main(String[] args) {
        try {
            // Grab the Scheduler instance from the Factory
            // 调度器
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            JobDetail job = newJob(HelloJob.class)
                    .withIdentity("job1", "group1")
                    .build();

            SimpleTrigger trigger = newTrigger().withIdentity("trigger1", "group1")
                    .startNow()
                    .withSchedule(simpleSchedule()
                            .withIntervalInSeconds(2)
                            .repeatForever())
                    .build();
            scheduler.scheduleJob(job, trigger);


            scheduler.start();
            Thread.sleep(6000);
            scheduler.shutdown();
        } catch (SchedulerException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class Test {

    public static void main(String[] args) throws SchedulerException {

        //创建一个scheduler
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.getContext().put("skey", "svalue");

        //创建一个Trigger
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                .usingJobData("t1", "tv1")
                .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?"))
//                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(3).repeatForever())
                .build();
        trigger.getJobDataMap().put("t2", "tv2");

        //创建一个job
        // HelloJob类可以与多个JobDetail实例进行关联，每一个实例都有自己的属性集和JobDataMap
        JobDetail job = JobBuilder.newJob(HelloJob.class)
                .usingJobData("j1", "jv1")
                .withIdentity("myjob", "mygroup").build();
        job.getJobDataMap().put("j2", "jv2");
        scheduler.scheduleJob(job,trigger);


        JobDetail dumbJob = newJob(DumbJob.class)
                .withIdentity("myJob", "group1")
                .usingJobData("jobSays", "Hello world!")
                .usingJobData("myFloatValue", 3.141f)
//                .storeDurably(true)
                .build();
        //注册trigger并启动scheduler
        CronTrigger trigger2 = newTrigger()
                .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?"))
                .withIdentity("trigger2", "group1")
                .forJob(dumbJob)
                .build();
        scheduler.scheduleJob(dumbJob,trigger2);
//        scheduler.addJob(dumbJob,false,false);
        scheduler.start();


        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        scheduler.shutdown();
    }

    static class triggerTest{

        public static void main(String[] args) throws SchedulerException, InterruptedException {

            JobDetail myJob = newJob(HelloJob.class)
                    .withIdentity("myJob", "group1")
                    .usingJobData("t1", "a")
                    .usingJobData("t2", "b")
                    .usingJobData("j1", "c")
                    .usingJobData("j2", "d")
                    .storeDurably(true)
                    .build();

            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            scheduler.addJob(myJob,true);
            HolidayCalendar cal = new HolidayCalendar();
            // 日历排除的日期不执行
            cal.addExcludedDate(Calendar.getInstance().getTime());
            cal.addExcludedDate(new Date());


            scheduler.addCalendar("myHolidays",cal,false,true);
            Trigger t = newTrigger()
                    .withIdentity("myTrigger")
                    .forJob("myJob")
                    .withSchedule(dailyAtHourAndMinute(9, 30)) // execute job daily at 9:30
                    .modifiedByCalendar("myHolidays") // but not on holidays
                    .build();

        // .. schedule job with trigger

            Trigger t2 = newTrigger()
                    .withIdentity("myTrigger2")
                    .forJob("myJob2")
                    .withSchedule(dailyAtHourAndMinute(11, 30)) // execute job daily at 11:30
                    .modifiedByCalendar("myHolidays") // but not on holidays
                    .build();

            SimpleTrigger simpleTrigger = newTrigger().withIdentity("trigger5", "group1")
//                    .startAt(futureDate(5, DateBuilder.IntervalUnit.MINUTE))
                    .withSchedule(simpleSchedule()
                            .withIntervalInSeconds(5)
                            .repeatForever()
                            // SimpleTrigger Misfire策略
                            .withMisfireHandlingInstructionNextWithExistingCount())
                    .forJob("myJob", "group1")
                    .endAt(dateOf(17, 30, 0))
                    .build();

            scheduler.scheduleJob(simpleTrigger);
            JobListener myJobListener = new MyJobListener();
            scheduler.getListenerManager().addJobListener(myJobListener, KeyMatcher.keyEquals(
                    new JobKey("myJob","group1")));
//            scheduler.getListenerManager().addJobListener(myJobListener, or(jobGroupEquals("myJobGroup"), jobGroupEquals("yourGroup")));

            // 注册SchedulerListeners监听器
            SchedulerListener mySchedListener = new MySchedListener();
            // 删除SchedulerListeners监听器
//            scheduler.getListenerManager().removeSchedulerListener(mySchedListener);

            scheduler.getListenerManager().addSchedulerListener(mySchedListener);
            scheduler.start();

//            Thread.sleep(5000);
        }
    }
}

